package com.mcxx.modules.miniSalvation.family.service.write;

import cn.hutool.core.util.IdUtil;
import com.mcxx.common.exception.BizException;
import com.mcxx.convert.TmpFundPayConvert;
import com.mcxx.modules.miniSalvation.family.dao.write.BankWriteDao;
import com.mcxx.modules.miniSalvation.family.dto.BankDTO;
import com.mcxx.modules.miniSalvation.family.dto.FamilyMemberDTO;
import com.mcxx.modules.miniSalvation.family.entity.BankEntity;
import com.mcxx.modules.miniSalvation.family.service.read.BankReadService;
import com.mcxx.modules.miniSalvation.order.dto.OrderCaregiversDTO;
import com.mcxx.modules.miniSalvation.order.dto.OrderDTO;
import com.mcxx.modules.miniSalvation.order.entity.OrderEntity;
import com.mcxx.modules.miniSalvation.order.service.read.OrderCertReadService;
import com.mcxx.modules.temporary.family.dto.TmpFamilyMemberDTO;
import com.mcxx.modules.temporary.order.dto.TmpAuditDTO;
import com.mcxx.modules.temporary.order.dto.TmpOrderDTO;
import com.mcxx.util.*;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("bankWriteService")
@TaTransactional
public class BankWriteService extends BaseService {
    @Autowired
    private BankWriteDao bankWriteDao;
    @Autowired
    private BankReadService bankReadService;



    public int insertIgnoreNull(BankEntity bankEntity) {
        return bankWriteDao.insertIgnoreNull(bankEntity);
    }


    public int addBankCopy(BankEntity bankEntity){
        return  bankWriteDao.addBankCopy(bankEntity);
    }


    public int updateIgnoreNull(BankEntity bankEntity) {
        return bankWriteDao.updateIgnoreNull(bankEntity);
    }


    public void updateBankDTO(TmpAuditDTO tmpAuditDTO, UserAccountVo userAccountVo){
        BankEntity bankEntity = TmpFundPayConvert.convertBankEntity(tmpAuditDTO, userAccountVo);
        int rst = bankWriteDao.updateByFamily(bankEntity);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_9999);
        }
    }


    public void backHistory(BankEntity bankEntityPage, BankEntity bankEntityDB){

        BankEntity updateBankEntity = new BankEntity();
        updateBankEntity.setId(bankEntityDB.getId());
        updateBankEntity.setStatus(Constant.BankStatus.STOP);
        int rst = this.updateIgnoreNull(updateBankEntity);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_9999);
        }


        bankEntityPage.setId(Constant.getUUID());
        bankEntityPage.setStatus(Constant.BankStatus.OK);
        bankEntityPage.setFamilyId(bankEntityDB.getFamilyId());
        bankEntityPage.setMemberId(bankEntityDB.getMemberId());
        bankEntityPage.setBusinessType(bankEntityDB.getBusinessType());
        bankEntityPage.setAreaCode(bankEntityDB.getAreaCode());
        bankEntityPage.setAgentCountyCode(bankEntityDB.getAgentCountyCode());
        rst = this.insertIgnoreNull(bankEntityPage);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_9999);
        }
    }


    public void updateBank(BankEntity bankEntityPage){
        this.updateIgnoreNull(bankEntityPage);



    }


    public String compareBank(BankEntity bankEntityPage, BankEntity bankEntityDB){

        if(CommonUtil.invalidArgs(bankEntityDB.getAgentAccount())){
            return "update";
        }


        Map<String, String> comparedProperties = new HashMap<String, String>(){{
            put("accountName","accountName");
            put("account","account");
            put("bankName","bankName");
            put("accountIdcard","accountIdcard");
            put("accountAddress","accountAddress");
            put("bankPhone","bankPhone");
        }};

        boolean compareRst = CompareUtils.isEuqal(bankEntityDB, bankEntityPage, comparedProperties);

        if(compareRst){
            return "nothing";
        }else{
            return "copy";
        }
    }


    public int insetBankList(List<BankEntity> baEntityList){
        return bankWriteDao.insetBankList(baEntityList);
    }





    public void addBankAccount(OrderEntity orderEntity, UserAccountVo accountVo) {

        List<BankEntity> bankEntityList = bankReadService.getBankByfamilyId(orderEntity.getFamilyId(),orderEntity.getBusinessType());
        if(CollectionUtils.isEmpty(bankEntityList)){

        List<BankEntity> lastRecordList = bankReadService.getByIdCardAndBizType(orderEntity.getFamilyId());

            if(CollectionUtils.isNotEmpty(lastRecordList)){
                BankEntity lastRecord = lastRecordList.get(0);

                if(Constant.BusinessCode.LINSHI.equals(lastRecord.getBusinessType())){

                    switch (orderEntity.getBusinessType()){
                        case "110":
                        case "130":
                            this.copyBankInsert(orderEntity, lastRecord, accountVo);
                            break;
                    }
                }else{

                    switch (orderEntity.getBusinessType()){
                        case "110":
                        case "130":
                            lastRecord.setBusinessType(orderEntity.getBusinessType());
                            bankWriteDao.updateIgnoreNull(lastRecord);
                            break;
                        case "500":
                            this.copyBankInsert(orderEntity, lastRecord, accountVo);
                            break;
                    }
                }
            }else{

                this.insertBank(orderEntity, accountVo);
            }
        }else{
            BankEntity lastRecord = bankEntityList.get(0);
            lastRecord.setMemberId(orderEntity.getMemberId());
            bankWriteDao.updateIgnoreNull(lastRecord);
        }
    }


    public void insertBank(OrderEntity orderEntity, UserAccountVo accountVo){
        BankEntity bankEntity = new BankEntity();
        bankEntity.setId(IdUtil.simpleUUID());
        bankEntity.setBusinessType(orderEntity.getBusinessType());
        bankEntity.setFamilyId(orderEntity.getFamilyId());
        bankEntity.setMemberId(orderEntity.getMemberId());
        bankEntity.setAreaCode(orderEntity.getAreaCode());
        //bankEntity.setCountyCode(orderEntity.getCountyCode());
        bankEntity.setCreateTime(DateUtil.now());
        bankEntity.setOrgId(accountVo.getOrgId());
        //bankEntity.setOperator(accountVo.getUserId());
        bankEntity.setStatus(Constant.BankStatus.OK);
        bankWriteDao.insertIgnoreNull(bankEntity);
    }


    public void copyBankInsert(OrderEntity orderEntity, BankEntity lastRecord, UserAccountVo accountVo){
        BankEntity bankEntity = new BankEntity();

        BeanUtils.copyProperties(lastRecord, bankEntity);

        bankEntity.setId(lastRecord.getId());
        bankEntity.setBusinessType(orderEntity.getBusinessType());
        bankEntity.setAreaCode(orderEntity.getAreaCode());
        //bankEntity.setCountyCode(orderEntity.getCountyCode());
        bankEntity.setCreateTime(DateUtil.now());
        bankEntity.setOrgId(accountVo.getOrgId());
        //bankEntity.setOperator(accountVo.getUserId());
        bankEntity.setStatus(Constant.BankStatus.OK);
        bankWriteDao.addBankCopy(bankEntity);
    }







    public void addBankAccountTmp(TmpAuditDTO tmpAuditDTO, UserAccountVo accountVo) {

        List<BankEntity> bankEntityList = bankReadService.getBankByfamilyId(tmpAuditDTO.getFamilyId(),tmpAuditDTO.getBusinessType());
        if(CollectionUtils.isEmpty(bankEntityList)){

            List<BankEntity> lastRecordList = bankReadService.getByIdCardAndBizType(tmpAuditDTO.getFamilyId());
            if(CollectionUtils.isNotEmpty(lastRecordList)){
                BankEntity lastRecord = lastRecordList.get(0);
                this.copyBankInsertTmp(tmpAuditDTO, lastRecord, accountVo);
            }else{
                this.insertBankTmp(tmpAuditDTO, accountVo);
            }
        }else{
            BankEntity lastRecord = bankEntityList.get(0);
            lastRecord.setMemberId(tmpAuditDTO.getMemberId());
            bankWriteDao.updateIgnoreNull(lastRecord);
        }
    }




    public void addBankAccountTmpCounty(TmpOrderDTO orderDTO, UserAccountVo accountVo) {
        List<BankEntity> bankEntityList = bankReadService.getBankByfamilyId(orderDTO.getFamilyId(),Constant.BusinessCode.LINSHI);
        if(CollectionUtils.isEmpty(bankEntityList)) {

            List<BankEntity> lastRecordList = bankReadService.getByIdCardAndBizType(orderDTO.getFamilyId());
            orderDTO.setBusinessType(Constant.BusinessCode.LINSHI);
            orderDTO.setAreaCode(accountVo.getAreaCode());
            if (CollectionUtils.isNotEmpty(lastRecordList)) {
                BankEntity lastRecord = lastRecordList.get(0);
                this.copyBankInsertTmpCounty(orderDTO, lastRecord, accountVo);
//                if (Constant.BusinessCode.LINSHI.equals(lastRecord.getBusinessType())) {












//                            lastRecord.setBusinessType(orderDTO.getBusinessType());







            } else {
                this.insertBankTmpCounty(orderDTO, accountVo);
            }
        }else{
                BankEntity lastRecord = bankEntityList.get(0);
                lastRecord.setMemberId(orderDTO.getMemberId());
                bankWriteDao.updateIgnoreNull(lastRecord);
            }
    }


    public void insertBankTmp(TmpAuditDTO tmpAuditDTO, UserAccountVo accountVo){
        BankEntity bankEntity = new BankEntity();
        bankEntity.setId(IdUtil.simpleUUID());
        bankEntity.setBusinessType(tmpAuditDTO.getBusinessType());
        bankEntity.setFamilyId(tmpAuditDTO.getFamilyId());
        bankEntity.setMemberId(tmpAuditDTO.getMemberId());
        bankEntity.setAreaCode(tmpAuditDTO.getAreaCode());
//        bankEntity.setCountyCode(QueryParamUtil.subAreaCode(tmpAuditDTO.getAreaCode()));
        bankEntity.setCreateTime(DateUtil.now());
        bankEntity.setOrgId(accountVo.getOrgId());
//        bankEntity.setOperator(accountVo.getUserId());
        bankEntity.setStatus(Constant.BankStatus.OK);

//        bankEntity.setBankName(tmpAuditDTO.getBankName());
//        bankEntity.setAccount(tmpAuditDTO.getAccount());
//        bankEntity.setBankPhone(tmpAuditDTO.getBankPhone());
//        bankEntity.setAccountName(tmpAuditDTO.getAccountName());
//        bankEntity.setAccountIdcard(tmpAuditDTO.getAccountIdcard());
//        bankEntity.setRelation(tmpAuditDTO.getRelation());

        bankWriteDao.insertIgnoreNull(bankEntity);
    }


    public void insertBankTmpCounty(TmpOrderDTO orderDTO, UserAccountVo accountVo){
        BankEntity bankEntity = new BankEntity();
        bankEntity.setId(IdUtil.simpleUUID());
        bankEntity.setBusinessType(orderDTO.getBusinessType());
        bankEntity.setFamilyId(orderDTO.getFamilyId());
        bankEntity.setMemberId(orderDTO.getMemberId());
        bankEntity.setAreaCode(orderDTO.getAreaCode());
//        bankEntity.setCountyCode(QueryParamUtil.subAreaCode(tmpAuditDTO.getAreaCode()));
        bankEntity.setCreateTime(DateUtil.now());
        bankEntity.setOrgId(accountVo.getOrgId());
//        bankEntity.setOperator(accountVo.getUserId());
        bankEntity.setStatus(Constant.BankStatus.OK);

//        bankEntity.setBankName(tmpAuditDTO.getBankName());
//        bankEntity.setAccount(tmpAuditDTO.getAccount());
//        bankEntity.setBankPhone(tmpAuditDTO.getBankPhone());
//        bankEntity.setAccountName(tmpAuditDTO.getAccountName());
//        bankEntity.setAccountIdcard(tmpAuditDTO.getAccountIdcard());
//        bankEntity.setRelation(tmpAuditDTO.getRelation());

        bankWriteDao.insertIgnoreNull(bankEntity);
    }


    public void copyBankInsertTmp(TmpAuditDTO tmpAuditDTO, BankEntity lastRecord, UserAccountVo accountVo){
        BankEntity bankEntity = new BankEntity();

        BeanUtils.copyProperties(lastRecord, bankEntity);

//        bankEntity.setId(IdUtil.simpleUUID());
        bankEntity.setBusinessType(tmpAuditDTO.getBusinessType());
        bankEntity.setAreaCode(tmpAuditDTO.getAreaCode());
//        bankEntity.setCountyCode(QueryParamUtil.subAreaCode(tmpAuditDTO.getAreaCode()));
        bankEntity.setCreateTime(DateUtil.now());
        bankEntity.setOrgId(accountVo.getOrgId());
//        bankEntity.setOperator(accountVo.getUserId());
        bankEntity.setStatus(Constant.BankStatus.OK);

//        bankEntity.setBankName(tmpAuditDTO.getBankName());
//        bankEntity.setAccount(tmpAuditDTO.getAccount());
//        bankEntity.setBankPhone(tmpAuditDTO.getBankPhone());
//        bankEntity.setAccountName(tmpAuditDTO.getAccountName());
//        bankEntity.setAccountIdcard(tmpAuditDTO.getAccountIdcard());
//        bankEntity.setRelation(tmpAuditDTO.getRelation());

        bankWriteDao.addBankCopy(bankEntity);
    }


    public void copyBankInsertTmpCounty(TmpOrderDTO orderDTO, BankEntity lastRecord, UserAccountVo accountVo){
        BankEntity bankEntity = new BankEntity();

        BeanUtils.copyProperties(lastRecord, bankEntity);

//        bankEntity.setId(IdUtil.simpleUUID());
        bankEntity.setBusinessType(orderDTO.getBusinessType());
        bankEntity.setAreaCode(orderDTO.getAreaCode());
//        bankEntity.setCountyCode(QueryParamUtil.subAreaCode(tmpAuditDTO.getAreaCode()));
        bankEntity.setCreateTime(DateUtil.now());
        bankEntity.setOrgId(accountVo.getOrgId());
//        bankEntity.setOperator(accountVo.getUserId());
        bankEntity.setStatus(Constant.BankStatus.OK);

//        bankEntity.setBankName(tmpAuditDTO.getBankName());
//        bankEntity.setAccount(tmpAuditDTO.getAccount());
//        bankEntity.setBankPhone(tmpAuditDTO.getBankPhone());
//        bankEntity.setAccountName(tmpAuditDTO.getAccountName());
//        bankEntity.setAccountIdcard(tmpAuditDTO.getAccountIdcard());
//        bankEntity.setRelation(tmpAuditDTO.getRelation());

        bankWriteDao.addBankCopy(bankEntity);
    }




    public int updateByOrderId(BankDTO bankDTO){
        return bankWriteDao.updateByOrderId(bankDTO);
    }


    public void editBankInfo(FamilyMemberDTO dto, UserAccountVo accountVo)  {

        if(Constant.DomesticRelation.ONESELF.equals(dto.getFamilyRelation())){

            this.resetBankAccountByFamilyId(dto.getFamilyId(),dto.getId());
            BankEntity bankEntity = new BankEntity();

            List<BankEntity> bankEntityList = bankReadService.getBankByMember(dto.getId(),dto.getBusinessType());
            if(CollectionUtils.isEmpty(bankEntityList)){
                bankEntity.setId(IdUtil.simpleUUID());
                bankEntity.setBusinessType(dto.getBusinessType());
                bankEntity.setFamilyId(dto.getFamilyId());
                bankEntity.setMemberId(dto.getId());
                bankEntity.setAreaCode(accountVo.getAreaCode());
                bankEntity.setCreateTime(DateUtil.now());
                bankEntity.setOrgId(accountVo.getOrgId());
                bankEntity.setStatus(Constant.BankStatus.OK);
                bankEntity.setIsAgent("0");
                bankEntity.setMasterSubsideProps(dto.getMasterSubsideProps());
                bankEntity.setMasterBankCode(dto.getMasterBankCode());
                bankEntity.setMasterAccount(dto.getMasterAccount());
                bankEntity.setAgentBankPhone(dto.getAgentBankPhone());
                bankEntity.setBankName(dto.getBankName());
                bankWriteDao.insertBankInfo(bankEntity);
            }else{
                BankEntity lastRecord = bankEntityList.get(0);
                bankEntity.setBusinessType(dto.getBusinessType());
                bankEntity.setFamilyId(dto.getFamilyId());
                bankEntity.setMemberId(dto.getId());
                bankEntity.setAreaCode(lastRecord.getAreaCode());
                bankEntity.setCreateTime(lastRecord.getCreateTime());
                bankEntity.setOrgId(lastRecord.getOrgId());
                bankEntity.setStatus(Constant.BankStatus.OK);
                bankEntity.setId(lastRecord.getId());
                bankEntity.setIsAgent("0");
                bankEntity.setMasterSubsideProps(dto.getMasterSubsideProps());
                bankEntity.setMasterBankCode(dto.getMasterBankCode());
                bankEntity.setMasterAccount(dto.getMasterAccount());
                bankEntity.setAgentBankPhone(dto.getAgentBankPhone());
                bankEntity.setBankName(dto.getBankName());
                bankWriteDao.updateBankInfo(bankEntity);
            }
        }
    }


    public void editTempBankInfo(TmpFamilyMemberDTO dto, UserAccountVo accountVo)  {

        if(Constant.DomesticRelation.ONESELF.equals(dto.getFamilyRelation())){

            this.resetBankAccountByFamilyId(dto.getFamilyId(),dto.getId());
            BankEntity bankEntity = new BankEntity();

            List<BankEntity> bankEntityList = bankReadService.getBankByMember(dto.getId(),dto.getBusinessType());
            if(CollectionUtils.isEmpty(bankEntityList)){
                bankEntity.setId(IdUtil.simpleUUID());
                bankEntity.setBusinessType(dto.getBusinessType());
                bankEntity.setFamilyId(dto.getFamilyId());
                bankEntity.setMemberId(dto.getId());
                bankEntity.setAreaCode(accountVo.getAreaCode());
                bankEntity.setCreateTime(DateUtil.now());
                bankEntity.setOrgId(accountVo.getOrgId());
                bankEntity.setStatus(Constant.BankStatus.OK);
                bankEntity.setIsAgent("0");
                bankEntity.setMasterSubsideProps(dto.getMasterSubsideProps());
                bankEntity.setMasterBankCode(dto.getMasterBankCode());
                bankEntity.setMasterAccount(dto.getMasterAccount());
                bankEntity.setAgentBankPhone(dto.getAgentBankPhone());
                bankEntity.setBankName(dto.getBankName());
                bankWriteDao.insertBankInfo(bankEntity);
            }else{
                BankEntity lastRecord = bankEntityList.get(0);
                bankEntity.setBusinessType(lastRecord.getBusinessType());
                bankEntity.setFamilyId(lastRecord.getFamilyId());
                bankEntity.setMemberId(dto.getId());
                bankEntity.setAreaCode(lastRecord.getAreaCode());
                bankEntity.setCreateTime(lastRecord.getCreateTime());
                bankEntity.setOrgId(lastRecord.getOrgId());
                bankEntity.setStatus(Constant.BankStatus.OK);
                bankEntity.setId(lastRecord.getId());
                bankEntity.setIsAgent("0");
                bankEntity.setMasterSubsideProps(dto.getMasterSubsideProps());
                bankEntity.setMasterBankCode(dto.getMasterBankCode());
                bankEntity.setMasterAccount(dto.getMasterAccount());
                bankEntity.setAgentBankPhone(dto.getAgentBankPhone());
                bankEntity.setBankName(dto.getBankName());
                bankWriteDao.updateBankInfo(bankEntity);
            }

        }
    }


    public void deleteBankInfo(String id){
        bankWriteDao.deleteBankInfo(id);
    }



    public void updateBankInfo(BankEntity dto,UserAccountVo accountVo) {
        BankEntity bankEntityDto = bankReadService.getBankByFamilyIdEdit(dto.getFamilyId(),dto.getMemberId(),dto.getBusinessType());
        BankEntity bankEntity = new BankEntity();
        if(null == bankEntityDto){
            bankEntity.setId(IdUtil.simpleUUID());
            bankEntity.setBusinessType(dto.getBusinessType());
            bankEntity.setFamilyId(dto.getFamilyId());
            bankEntity.setMemberId(dto.getMemberId());
            bankEntity.setAreaCode(accountVo.getAreaCode());
            bankEntity.setCreateTime(DateUtil.now());
            bankEntity.setOrgId(accountVo.getOrgId());
            bankEntity.setStatus(Constant.BankStatus.OK);
            bankEntity.setAgentBankPhone(dto.getMasterPhone());
            if(Constant.IsAgent.YES.equals(dto.getIsAgent())){
                bankEntity.setIsAgent(dto.getIsAgent());
                bankEntity.setAgentCountyCode(dto.getAgentCountyCode());
                bankEntity.setAgentVillageCode(dto.getAgentVillageCode());
                bankEntity.setAgentOperator(dto.getAgentOperator());
//                bankEntity.setAgentBankPhone(dto.getAgentBankPhone());
                bankEntity.setAgentRelation(dto.getAgentRelation());
                bankEntity.setAgentCensusType(dto.getAgentCensusType());
                bankEntity.setAgentSubsideProps(dto.getAgentSubsideProps());
                bankEntity.setAgentAccount(dto.getAgentAccount());
                bankEntity.setAgentAccountIdcard(dto.getAgentAccountIdcard());
                bankEntity.setAgentBankCode(dto.getAgentBankCode());
                bankEntity.setAgentMemberAddress(dto.getAgentMemberAddress());
                bankEntity.setAgentMemberNumber(dto.getAgentMemberNumber());
            }else if(Constant.IsAgent.NO.equals(dto.getIsAgent())){
                bankEntity.setIsAgent(dto.getIsAgent());
                bankEntity.setMasterSubsideProps(dto.getMasterSubsideProps());
                bankEntity.setMasterBankCode(dto.getMasterBankCode());
                bankEntity.setMasterAccount(dto.getMasterAccount());
                bankEntity.setBankName(dto.getBankName());
            }
            bankWriteDao.insertBankInfo(bankEntity);

        }else{
            dto.setId(bankEntityDto.getId());
                if("0".equals(dto.getIsAgent())){
                    dto.setAgentOperator("");
                    dto.setAgentAccountIdcard("");
                    dto.setAgentRelation("");
                    dto.setAgentCountyCode("");
                    dto.setAgentVillageCode("");
                    dto.setAgentBankPhone(dto.getMasterPhone());
                    dto.setAgentCensusType("");
                    dto.setAgentMemberAddress("");
                    dto.setAgentSubsideProps("");
                    dto.setAgentBankCode("");
                    dto.setAgentAccount("");
                    dto.setAgentMemberNumber(null);
                }else{
                    dto.setMasterSubsideProps("");
                    dto.setMasterBankCode("");
                    dto.setMasterAccount("");
                }
            this.updateBank(dto);}

    }


    public int resetBankAccountByFamilyId(String familyId,String memberId){
        return bankWriteDao.resetBankAccountByFamilyId(familyId,memberId);
    }

    public int updateByFamily(BankEntity entity){
       return  bankWriteDao.updateByFamily(entity);
    }

    public int updateBankPhone(BankEntity bankEntity){
        return bankWriteDao.updateBankPhone(bankEntity);
    }
}
